<?php
Zend::loadClass('Lead_EDO_Object_Base');
Zend::loadClass('Lead_EDO_Object_Helper_Static');

/**
 * @package EDO
 * @subpackage Core
 *
 */

// require_once dirname(__FILE__) . '/class.StaticEDOHelper.php';

/**
 * Abstract EDO object base class.
 *
 */
abstract class Lead_EDO_Object_Basic extends Lead_EDO_Object_Base
{

	static $_queries = array();

	static $store = array();

	private $isFetched = false;

	public function __construct(Lead_EDO_Handler_Interface $edo_handler)
	{
		parent::__construct($edo_handler);
		$this->_typeData = Lead_EDO_Object_Helper_Static::getTypeData($this);
	    $this->_initDataTypes();
	}


	
	/**
	 * Gets a new EDO of object's class. If edoid is defined, the new object is fetched.
	 *
	 * @param integer $edoid
	 * @return EDO_object_basic
	 */
	public function get($edoid = null)
	{
	    $obj = $this->getHandler()->get($this->getClass());
	    if($edoid)
	       $obj->fetch($edoid);
	    return $obj;
	}
	
		

	


	/**
	 * Fetches a dataobject.
	 *
	 * @param unknown_type $edoid
	 */
	function fetch($edoid)
	{
		$this->triggerEvent('beforeFetch');

		if(key_exists($edoid, Lead_EDO::$store))
		{
		    $this->setFields(Lead_EDO::$store[$edoid]);
            $this->setFetched(true); // this must be set, since it's a property rather than a classvar
			$this->triggerEvent('afterFetch');
					
			return true;

		 }
		 else
		 {
		     
		   $phrase = $this->getHandler()->getFetchQuery($this) . "WHERE edo_objects.edoid = ?";
		   
 		   $prepared = $this->ldb->prepare($phrase);

		   $prepared->execute(array($edoid));
		   $row = $prepared->fetch();
		   $prepared->closeCursor();

			
			if($row)
			{
    
			    
				Lead_EDO::$store[$edoid] = $row;

				$this->setFields($row);
				$this->setFetched(true);
								
				$this->triggerEvent('afterFetch');
				
				return true;

			}
			else
			{
				$this->triggerEvent('fetchFailed');
				$this->setFetched(false);
			
				
				return false;
			}
		 }
		 
		 return true;
	}

	/**
	 * Unfetches an object
	 *
	 */
	public function unfetch()
	{
		$this->triggerEvent('onUnfetch');
		$this->setFetched(false);
	}


	/**
	 * Sets object's fetched state.
	 *
	 * @param boolean $fetchedBool true or false
	 */
	public function setFetched($fetchedBool)
	{
		$this->isFetched = $fetchedBool;
	}

	/**
	 * Returns true if object state is fetched, false if not
	 *
	 * @return boolean true or false
	 */
	public function isFetched()
	{
		return $this->isFetched;
	}


	/**
	 * Returns a searcher for this dataobject
	 *
	 * @return EDO_searcher
	 */
	function getSearcher()
	{
		require_once EDO::getRoot() . '/Core/Searcher/class.EDO_searcher.php';
		$searcher = new EDO_searcher($this);

		return $searcher;
	}

	/**
	 * Quicksearches.
	 *
	 * @param unknown_type $params
	 * @param unknown_type $operator
	 * @param unknown_type $config
	 * @return unknown
	 */
	function quickSearch($params = array(), $operator = "=", $config = array())
	{

		$searcher = $this->getSearcher();
		
		return $searcher->quickSearch($params, $operator, $config);
	}



	/**
	 * Returns a collection.
	 *
	 * @param unknown_type $keys
	 * @param unknown_type $collectionType
	 * @return unknown
	 */
	public function collection($keys = array(), $collectionType = 'basic')
	{
		return $this->edo->collection($this->getClass(), $keys, $collectionType);

	}


	/**
	 * Fetches and returns mass data from the dataobject.
	 *
	 * @param string $fieldName Field name
	 * @param array $config Config array
	 * @return array
	 */
	public function fetchMassData(EDO_object_basic $relatedObj, $fieldName, $config = array())
	{
	   	    	    
	    static $massdata;
		if(!$massdata)
			$massdata = array();

		if(!isset($massdata[$relatedObj->getClass()][$fieldName]))
		{
		    $fObj = $relatedObj->getFieldObject($fieldName);
            $massdata[$relatedObj->getClass()][$fieldName] = $fObj->fetchMassData($this, $config);
		}

		return $massdata[$relatedObj->getClass()][$fieldName];
	}



	/**
	 * Returns dataobjects unique typeid.
	 *
	 * @return integer
	 */
	public function getTypeid()
	{
        return $this->getHandler()->convert($this->getClass());
	}


	
	/**
	 * Parses a display string. Each %variable in $displayStr is forwarded to the
	 * _displayStringResolver helper method.
	 *
	 * @param string $displayStr Display string
	 * @return string Parsed string.
	 */
	public function parseDisplayString($displayStr)
	{
        $regexp = "/(\%([a-zA-Z_\>\-]*))/si";
        $resolved = preg_replace_callback($regexp, array($this, '_displayStringResolver'), $displayStr);
        return $resolved;
    }

	    
    /**
     * Display string parser's helper method parses a path with the FieldPath
     * object.
     *
     * @param unknown_type $matches
     * @return unknown
     */
    private function _displayStringResolver($matches)
    {        
        static $fieldPath;
        
        if(!$fieldPath)
        {
            LEAD::requirePackage('EDO/Tools/FieldPath');
            $fieldPath = new FieldPath();
        }
                               
        return $fieldPath->resolve($this, $matches[2], '->');
    }




}
?>
